package com.yema.mytestlibrary.http

import android.os.Bundle
import android.os.Environment
import android.util.Log
import androidx.fragment.app.FragmentActivity
import com.yema.retrofittest.Http.RetrofitMamnager
import com.zdl.netutils.Http.DownFileKotlin
import com.zdl.netutils.Http.DownloadListener
import kotlinx.android.synthetic.main.activity_http.*
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.io.File
import kotlin.concurrent.thread

/**
 *
 * 文件描述
 * 用户名:yema
 * 时间:2019/7/31   11:50
 */
class HttpTestAc : FragmentActivity() {
    var fieurl: String = "http://fabu.jsti-smart.cn/upload/5d2bf3a12692430018614e89/android/com.yemast.road.witconstructionsite_1.6.6_66.apk"


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(com.yema.mytestlibrary.R.layout.activity_http)


        val externalStorageDirectory = Environment.getExternalStorageDirectory()
        Log.e("tag", "aa${externalStorageDirectory.path}");
        val downloadCacheDirectory = Environment.getDownloadCacheDirectory()
        println("aaa==$downloadCacheDirectory")
        Log.e("", "aaaaa=${downloadCacheDirectory.path}");

        var file = File("${Environment.getExternalStorageDirectory()}/down")
        if (!file.mkdirs()) {
            file.createNewFile()
        }
        val open = assets.open("飞标采集.docx")
        var file2 = File("${file.path}/aa.docx")





        file2.deleteOnExit()
        file2.createNewFile()
        var c = -1
        var buffer = ByteArray(1024)
        Log.e("tag", "currenttiem===${System.currentTimeMillis()}");
        while ({ c = open.read(buffer);c }() > 0) {
            file2.appendBytes(buffer.copyOfRange(0, c))
        }
        Log.e("tag", "currenttiem===${System.currentTimeMillis()}");






        tv_one.setOnClickListener {

            thread {
                val execute = RetrofitMamnager.getUrlApi().getExplain("about_us").execute()
                Log.e("tag", execute.body().toString());
            }


        }
        tv_two.setOnClickListener {
            thread {
                val execute = RetrofitMamnager.getMyApi().servers().execute()
                Log.e("tag", execute.body().toString());
            }
        }


        tv_down.setOnClickListener {

            RetrofitMamnager.getdownapi().downFile(fieurl).enqueue(object : Callback<ResponseBody> {
                override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                }

                override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                    DownFileKotlin.writeResponseBodyToDisk2(fieurl, "down", response, object : DownloadListener {
                        override fun onDownloadSuccess(path: String) {
                        }

                        override fun onDownloading(progress: Long) {
                            Log.e("tag", "$progress");
                        }

                        override fun onDownloadFailed() {
                            Log.e("tag", "");

                        }
                    })
                }
            })


        }
    }
}
